package top.infopub.mgr.fia.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.fia.InvoiceTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.domain.InvoiceManageDomain;
import top.infopub.mgr.fia.jsonvo.InvoiceManageJsonVo;
import top.infopub.mgr.fia.service.FiaProcessManageService;
import top.infopub.mgr.fia.service.InvoiceManageService;
import top.infopub.mgr.fia.vo.InvoiceManageVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.model.fia.InvoiceManage;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/invoiceManage")
public class InvoiceManageController extends GenericFlowController {
    @Autowired
    private InvoiceManageService invoiceManageService;

    @Autowired
    private FiaProcessManageService fiaProcessManageService;

    /**
     * 发票管理列表
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        List<Map<String, String>> invoiceType= InvoiceTypeEnum.asList();
        request.setAttribute("invoiceType",invoiceType);
        return "fia/lay-invoiceManage-list";
    }

    /**
     * 发票管理列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<InvoiceManageDomain> queryDataByCondition(InvoiceManageVo invoiceManageVo, ParamPage paramPage) {
        Page<InvoiceManageDomain> page = new Page<InvoiceManageDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = invoiceManageService.selectPageByDomainWhere(page, invoiceManageVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id SGUID
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        if (StringUtils.isNotBlank(id)) {
            InvoiceManage invoiceManage = invoiceManageService.selectByPrimaryKey(id);
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            if (invoiceManage != null) {
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), invoiceManage.getProcessStatus())
                        || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), invoiceManage.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                }
                if (StringUtils.isNotBlank(invoiceManage.getProcessId()) && invoiceManage.getProcessId().length() > 10) {
                    request.setAttribute("processId", invoiceManage.getProcessId());
                    String auditTabFlag = fiaProcessManageService.checkShowAuditTab(invoiceManage.getProcessId(), vo);
                    if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), invoiceManage.getProcessStatus())
                            && StringUtils.equals("0", auditTabFlag)) {
                        request.setAttribute("isAuditFlag", "0");
                    }
                    request.setAttribute("auditTabFlag", auditTabFlag);
                }
            }
        }
        request.setAttribute("proCode", proCode);
        return "fia/lay-InvoiceManagement-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        InvoiceManageJsonVo invManJson = invoiceManageService.queryDetailById(id);
        return RspVo.getSuccessResponse(invManJson);
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String processId, String status, String auditMsg, String id) {
        //审批
        fiaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }

    /**
     * 编辑
     * @param bodyStr json数据
     * @param submitType 0=保存，1=提交
     * @return
     */
    @RequestMapping("/edit")
    public @ResponseBody RspVo edit(String bodyStr, Integer submitType) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        InvoiceManageJsonVo invMan = JSON.parseObject(str, InvoiceManageJsonVo.class);
        RspVo rspVo = invoiceManageService.edit(invMan.getInfo(), submitType, vo);
        return rspVo;
    }
}
